package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.InspectiontaskDetailsDao;
import org.elepower.dao.Inspectiontask_mainDao;
import org.elepower.dao.PsInspectorDetailsDao;
import org.elepower.dao.SystemConfigDao;
import org.elepower.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/taskReturn")
public class TaskReturnController {
    @Autowired
    private Inspectiontask_mainDao inspectiontask_mainDao;
    @Autowired
    private SystemConfigDao systemConfigDao;
    @Autowired
    private InspectiontaskDetailsDao inspectiontaskDetailsDao;
    @Autowired
    private PsInspectorDetailsDao inspectorDetailsDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/inspectionTaskReturn/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, PsInspectiontask_main condition, HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<>();

        Page<PsInspectiontask_main> pager = PageHelper.startPage(page, limit);
        List<PsInspectiontask_main> list = inspectiontask_mainDao.getNoCancelTask(condition);
        request.setAttribute("list",list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", pager.getTotal());
        return map;
    }

    @RequestMapping("/returnTaskView")
    public String returnTaskView(String taskId,HttpSession session) throws Exception{
        session.setAttribute("taskId",Integer.parseInt(taskId));
        return "system/inspectionTaskReturn/returnTask";
    }

    @RequestMapping("/getBugType")
    @ResponseBody
    public List<PsSysteamconfig> getBugType() throws Exception{
        return systemConfigDao.getBugType();
    }

    @RequestMapping("/getBugLevel")
    @ResponseBody
    public List<PsSysteamconfig> getBugLevel() throws Exception{
        return systemConfigDao.getBugLevel();
    }

    @RequestMapping("/updateTaskInfo")
    @ResponseBody
    public Map<String,Object> updateTaskInfo(PsInspectiontaskDetails psInspectiontaskDetails,String time,HttpSession session) throws Exception{
        Map<String, Object> map = new HashMap<>();
        PsUser psUser = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //根据编号查询缺陷类型和缺陷级别
        PsSysteamconfig bugType = systemConfigDao.getBugTypeId(psInspectiontaskDetails.getBugTypeId());
        PsSysteamconfig bugLevel = systemConfigDao.getBugLevelId(psInspectiontaskDetails.getBugLevelId());
        //完善缺陷信息
        psInspectiontaskDetails.setIsBug(1);
        psInspectiontaskDetails.setBugLevelName(bugLevel.getConfigValueName());
        psInspectiontaskDetails.setBugTypeName(bugType.getConfigValueName());
        psInspectiontaskDetails.setDiscovererCode(psUser.getName());
        psInspectiontaskDetails.setDiscovererName(psUser.getUsername());
        psInspectiontaskDetails.setIsSolve(0);
        psInspectiontaskDetails.setIsBugLevel(0);
        psInspectiontaskDetails.setDiscoverTime(format.parse(time));
        psInspectiontaskDetails.setInspectTime(new Date());
        psInspectiontaskDetails.setLastUpdateTime(new Date());
        //修改缺陷信息
        inspectiontaskDetailsDao.update(psInspectiontaskDetails);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(Integer taskId) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //更新巡检任务主表状态
        inspectiontask_mainDao.updateTaskStateAll(taskId,4,"已完成");
        //更新巡检员状态
        inspectorDetailsDao.updateInspectorTaskId(taskId);
        map.put("result",true);
        return map;
    }

}
